package com.ly.algorithm.DataStructures.tree;


/**
 * @Classname BinaryTree
 * @Description TODO
 * @Date 2020/4/25 15:56
 * @Author 冷心影翼
 */
public class BinaryTree  {
    public Node tree;

    public Node preOrderSearch(int no) {
        if(tree!=null) {
            return tree.preOrderSearch(no);
        }else {
            return null;
        }
    }
    public Node infixOrderSearch(int no) {
        if(tree!=null) {
            return tree.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    public Node postOrderSearch(int no) {
        if(tree!=null) {
            return tree.postOrderSearch(no);
        }else {
            return null;
        }
    }

    public BinaryTree(Node tree) {
        this.tree = tree;
    }

    public void preOrder() {
        if(tree!=null){
            tree.preOrder();
        }

    }

    public void infixOrder() {
        if(tree!=null){
            tree.infixOrder();
        }

    }

    public void postOrder() {
        if(tree!=null){
            tree.postOrder();
        }
    }

    public void delete(int no) {
        if(tree!=null) {
            tree.delete(no);
        }
        if(tree.no == no) {
            tree = null;
        }
    }


}

class Test{
    public static void main(String[] args) {


        Node a = new Node(1, "a");
        Node b = new Node(2, "b");
        Node c = new Node(3, "c");
        Node d = new Node(4, "d");
        Node e = new Node(5, "e");
        a.right = b;
        b.right = c;
        b.left = d;
        d.right = e;


        BinaryTree tree = new BinaryTree(a);
//        tree.preOrder();
//        System.out.println();
//        tree.infixOrder();
//        System.out.println();
//        tree.postOrder();
//        System.out.println();
//        System.out.println("------");

//        System.out.println(tree.preOrderSearch(3));
//        System.out.println(tree.postOrderSearch(3));
//        tree.delete(3);
        tree.postOrder();
    }
}

class Node {
    public int no;

    public String name;

    public Node left;

    public Node right;

    // 0 -> 左子树  1.txt -> 前驱节点
    public int leftType;

    // 0 -> 右子树  1.txt -> 后继节点
    public int rightType;


    public Node parent;

    public Node(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                ", name='" + name ;
    }

    public void preOrder() {
        System.out.println(this);
        if(this.left!=null) {
            this.left.preOrder();
        }
        if(this.right!=null) {
            this.right.preOrder();
        }
    }

    public void infixOrder() {

        if(this.left!=null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null) {
            this.right.infixOrder();
        }
    }

    public void postOrder() {
        if(this.left!=null) {
            this.left.postOrder();
        }
        if(this.right!=null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }

    public Node preOrderSearch(int no) {
        Node result = null;
        if(this.no == no) {
            return this;
        }
        if(this.left!=null) {
            result =  this.left.preOrderSearch(no);
        }
        if(result!=null) {
            return result;
        }
        if(this.right!=null) {
            result =  this.right.preOrderSearch(no);
        }
        return result;
    }

    public Node infixOrderSearch(int no) {
        Node result = null;
        if(this.left!=null ) {
            result = this.left.infixOrderSearch(no);
        }
        if(result!=null) {
            return result;
        }
        if(this.no == no) {
            return this;
        }
        if(this.right!=null) {
            result = this.right.infixOrderSearch(no);
        }
        return result;
    }

    public Node postOrderSearch(int no) {
        Node result = null;
        if(this.left!=null) {
            result =  this.left.postOrderSearch(no);
        }
        if(result!=null) {
            return result;
        }
        if(this.right!=null) {
            return this.right.postOrderSearch(no);
        }
        if(result!=null) {
            return result;
        }
        if(this.no == no) {
            result =  this;
        }
        return result;
    }

    public void delete(int  no) {
        if(this.left != null) {
            if(this.left.no == no) {
                this.left = null;
                return;
            }else{
                this.left.delete(no);
            }
        }
        if(this.right != null) {
            if(this.right.no == no) {
                this.right = null;
                return;
            }else{
                this.right.delete(no);
            }
        }

    }


}
